/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.vtutor.check.object;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPFile;
import ru.miit.elearning.vtutor.data.CheckObject;
import ru.miit.elearning.vtutor.ftp.FtpEntry;
import ru.miit.elearning.vtutor.ftp.FTPClientDecorator;

/**
 *
 * @author Cyclone
 */
public class CheckObjectPSType extends ZOSAbstractCheckObject {
    CheckObjectPSType(CheckObject co) {
        super(co);
    }

    @Override
    public String getTextContent(FTPClientDecorator ftp) {
        ByteArrayOutputStream baos = null;
        String textContent = null;
        try {
            baos = new ByteArrayOutputStream();
            if (ftp.retrieveFile(path, baos)) {
                textContent = baos.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ex) {
                    System.err.println(ex.getMessage());
                    Logger.getLogger(CheckObjectFileType.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return textContent;
    }

    @Override
    protected boolean isObjectListed(FTPClientDecorator ftp) {
        Map<String, FtpEntry> entryMap = ftp.getEntryMap();
        FtpEntry entry = entryMap.get(path.toUpperCase());

        return entry != null && (entry.getType() == null || entry.getType().equals(FTPFile.FILE_TYPE));
    }
}
